package chat.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ChatLoginActivity extends Activity {

	private static int PORT = 5555;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        final EditText ipadress = (EditText) findViewById(R.id.ipAdress);
        final EditText userName = (EditText) findViewById(R.id.userName);
        final TextView error = (TextView) findViewById(R.id.error);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
/*				try {
					((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(300);
					progressBar.setVisibility(ProgressBar.VISIBLE);
					//Checking IP
					InetAddress ip = InetAddress.getByName(ipadress.getText().toString());
					//Create Socket to server
					Socket connect = new Socket(ip, PORT);
					
					//Read and write from server
	    			BufferedReader socketReader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
	    			
	    			while(!socketReader.ready()){ }
	    			String line = socketReader.readLine();
	    			int port;
	    			try{
	    				port = Integer.parseInt(line);
	    			} catch(NumberFormatException e){
						error.setText("Number format on string: "+ line);
						return;
	    			}
	    			connect.setReuseAddress(true);
	    			connect.close();
	    			connect = null;
	    			
	    			ServerSocket serv = new ServerSocket(port);
	    			connect = serv.accept();
	    			
	    			//Writing username to server
					PrintWriter socketWriter = new PrintWriter(connect.getOutputStream(),true);
					socketReader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
	    			socketWriter.println(userName.getText());
	    			while(!socketReader.ready()){ }
	    			if(!socketReader.readLine().equals("Username available")){
	    				error.setText("Username already taken");
	    			} else {
						Intent startChatWindow = new Intent(v.getContext(), ChatClientActivity.class);
						Save s = (Save)getApplication();
						s.setSocket(connect);
						s.setUserName(userName.getText().toString());
		                startActivity(startChatWindow);
	    			}
				} catch (UnknownHostException e) {
					error.setText("Invalid Ip address");
				} catch (IOException e) {
					error.setText("Could not connect to server");
				}
				progressBar.setVisibility(ProgressBar.INVISIBLE);*/
				Intent startChatWindow = new Intent(v.getContext(), ChatClientActivity.class);
				startActivity(startChatWindow);
			}
		});
		
	}
	
}
